package weixin

import (
	"github.com/astaxie/beego/logs"
	"fmt"
	"strconv"
)

type Handler interface {
	DoHandler(msg *WeiXinMsg) (replyMsg *WeiXinMsg, canHandler bool)
	Priority() int
	SetPriority(priority int)
	Helper() (map[string]string)
}

type HelpHandler struct {
	Pri int
}

type DefaultHandler struct {
	Text CDATA
	Pri  int
}

type NewsHandler struct {
	Text CDATA
	Pri  int
}

func (this DefaultHandler) DoHandler(msg *WeiXinMsg) (replyMsg *WeiXinMsg, canHandler bool) {
	canHandler = true
	logs.Info("DefaultHandler 回复微信消息")
	if msg.MsgType != WX_TEXT {
		switch msg.MsgType {
		case WX_IMAGE:
			replyMsg = msg.ReplayText("图片消息:" + msg.PicUrl + " MediaId:" + msg.MediaId)
		case WX_VOICE:
			replyMsg = msg.ReplayText("语音消息:" + msg.Format + " MediaId:" + msg.MediaId)
		case WX_VIDEO:
			replyMsg = msg.ReplayText("视频消息:" + msg.ThumbMediaId + " MediaId:" + msg.MediaId)
		case WX_SHORTVIDEO:
			replyMsg = msg.ReplayText("小视频消息:" + msg.ThumbMediaId + " MediaId:" + msg.MediaId)
		case WX_LOCATION:
			replyMsg = msg.ReplayText(CDATA(fmt.Sprintf("地理位置消息: %s->[%f,%f]", msg.Label, msg.Location_X, msg.Location_Y)))
		case WX_LINK:
			replyMsg = msg.ReplayText(CDATA(fmt.Sprintf("链接消息: %s,%s,%s", msg.Title, msg.Description, msg.Url)))
		default:
			replyMsg = msg.ReplayText(CDATA(msg.ToXml()))
		}
	} else {
		switch msg.Content {
		case "文字":
			replyMsg = msg.ReplayText(this.Text + msg.Content)
		case "音乐":
			replyMsg = msg.ReplayMusic("我吃飞泥度", "街篮音乐", "http://106.15.194.157/jl/music/我吃飞你度_铃声之家cnwav.mp3")
		case "图文":
			replyMsg = msg.ReplayNews()
			replyMsg.AppendNews("黑桃先生：这个教学千万别让布莱恩看到！", "别让", "http://mmbiz.qpic.cn/mmbiz_jpg/wRAjibSKfzg6SWGlFUJibybAySamx4nuGdKkEF1I1ap12woHH0zDJuHH76XlW0PL3qHDINaYmqNkMia80vYb21cBw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1", "https://mp.weixin.qq.com/s?__biz=MzI1NTIxMTExNg==&mid=2247486094&idx=1&sn=e4e9690d39d90f17d3fa6b1cc120dd18&chksm=ea38298ddd4fa09bbf550b80f678f2c9a1ea16aa85867e3da912ac642044fdcf1dff3b33ceff&mpshare=1&scene=23&srcid=0702qEWvXYLv0c3q4DlX4lgw#rd")
		default:
			replyMsg = msg.ReplayText(this.Text + msg.Content)
		}
	}
	replyMsg.ToXml()
	return
}

func (this DefaultHandler) Priority() int {
	return this.Pri
}

func (this DefaultHandler) Helper() map[string]string {
	help := make(map[string]string)
	helps := `文字|图文`
	help["test"] = helps
	help[strconv.Itoa(this.Pri)] = helps
	return help
}

func (this DefaultHandler) SetPriority(priority int) {
	this.Pri = priority
}

func (this NewsHandler) DoHandler(msg *WeiXinMsg) (replyMsg *WeiXinMsg, canHandler bool) {
	if msg.MsgType != WX_NEWS {
		return
	}
	canHandler = true
	//replyMsg = &msg.ReplayNews()
	/*replyMsg.*/
	return
}

func (this NewsHandler) Priority() int {
	return this.Pri
}

func (this NewsHandler) SetPriority(priority int) {
	this.Pri = priority
}
